<template>
	<!-- 校友通讯录 -->
	<g-page :is-tabbar="false">
		<view class="content">
			<!-- 搜索 -->
			<view class="search">
				<!-- 搜索框参数：https://uviewui.com/components/search.html -->
				<u-search shape="round" placeholder="输入姓名"></u-search>
			</view>
			<view v-if="authen !==undefined && authen.length > 0">
				<view class="tr" v-for="(item,index) in authen" :key="index">
					<view class="top">
						<image class="image-bg" :src="item.avatar_url" @error="errorImg(index)"
							style="width: 150rpx;height: 150rpx;border-radius: 50%;"></image>
						<view class="name">{{item.username}}</view>
						<view class="td" v-if="item.phone===null">联系电话：暂无</view>
						<view class="td" v-else>联系电话：{{item.phone}}</view>
						<view class="td1">申请添加我为通讯录校友</view>
						<button class="clik" @click="agree(item.id,index)">同意</button>
						<button class="clik1" @click="ignore(item.id,index)">忽略</button>
					</view>
				</view>
			</view>
			<view v-else>
				<view>
					<view class="nothing">{{message}}</view>
				</view>
			</view>
		</view>
	</g-page>
</template>

<script>
	import {
		getContacts
	} from "@api"
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				page: 0,
				size: 100,
				mailList: [{
					name: '严晓波',
					work_unit: '工程实训中心',
					phone: '18278871121'
				}],
				userInfo: {}
			};
		},
		computed: {
			...mapState(['USER'])
		},
		onLoad() {
			let that = this;
			
			that.userInfo = that.USER.userInfo;
			uni.request({
					url: this.$t.URL.api + '/gzmuAnnUserContacts/apply',
					data: {
						myId: that.userInfo.id,
						page: that.page,
						size: that.size
					},
					method: 'GET'
				})
				.then(data => { //data为一个数组，数组第一项为错误信息，第二项为返回数据
					var [error, res] = data;
					this.authen = res.data;
					console.log(this.authen);
				})
		},
		onReachBottom: function() { //当划到最底部的时候触发事件
			this.getMoreNews(); //到底之后会触发getMoreNews()函数;
		},
		methods: {
			errorImg(index) {
				this.authen[index].head_img_url = '../../static/images/define.png'; //默认图片  
			},
			agree: function(id, index) {
				//localhost:8000/api/gzmuAnnUserContacts/alter?contactId=2&consent=1
				console.log(id);
				console.log(index);
				uni.request({
					url: this.$t.URL.api + '/gzmuAnnUserContacts/alter?contactId=' + id + '&consent=1',
					method: 'GET'
				});
				this.authen.splice(index, 1);
				this.getMoreNews();

			},
			ignore: function(id, index) {
				console.log(id);
				console.log(index);
				uni.request({
					url: this.$t.URL.api + '/gzmuAnnUserContacts/alter?contactId=' + id + '&consent=2',
					method: 'GET'
				});
				this.authen.splice(index, 1);
				this.getMoreNews();
			},
			getMoreNews: function() { //数据到底,触发这个函数
				this.page = this.page + 1
				uni.showNavigationBarLoading(); //一读取数据,就进行刷新
				uni.request({
						url: this.$t.URL.api + '/gzmuAnnUserContacts/apply?myId=' + this.id + "&page=" +
							this.page + "&size=" +
							this.size,
						method: 'GET'
					})
					.then(data => { //data为一个数组，数组第一项为错误信息，第二项为返回数据
						var [error, res] = data;
						console.log(this.page);
						if (res.data == 0) {
							this.page = this.page - 1
							uni.stopPullDownRefresh(); //数据加载完成,刷新结束
							uni.hideNavigationBarLoading(); //数据读取完毕,刷新停止
							return false;
						}
						this.authen = this.authen.concat(res.data)
						console.log(this.authen);
						uni.stopPullDownRefresh(); //数据加载完成,刷新结束
						uni.hideNavigationBarLoading(); //数据读取完毕,刷新停止

					});
			},

		}
	}
</script>

<style lang="scss">
	.content {
		padding: 10rpx 20rpx;
	}

	.search {
		margin-bottom: 20rpx;
	}

	.item {
		&-item {
			height: 120rpx;
			line-height: 120rpx;
			padding: 2rpx 20rpx;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
		}

		&-left {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			height: 80rpx;
			line-height: 80rpx;

			&-t {
				display: flex;
				flex-direction: column;
				margin-left: 30rpx;
				margin-top: 20rpx;

				&-name,
				&-dep {
					height: 40rpx;
					line-height: 40rpx;
				}

				&-name {
					font-size: 1.1em;
				}

				&-dep {
					font-size: 24rpx;
					color: #96969a;
				}
			}
		}

		&-box {
			&:hover {
				background: #F6F6F6 !important;
			}
		}

		&-icon {
			width: 80rpx;
			height: 80rpx;
			margin-top: 20rpx;
			border-radius: 50%;
			background-color: #ff0000;
		}

		&-right {
			height: 120rpx;
			line-height: 120rpx;
		}
	}
</style>

<script>
	let app = getApp();
	import {
		mapState,
		mapMutations
	} from 'vuex'

	export default {
		data() {
			return {
				authen: [],
				id: 2,
				size: 10,
				page: 0,
				message: '暂无校友申请'
			}
		},
		computed: {
			...mapState(['userId', 'token', 'userName', 'wxNickName'])
		},
		
	}
</script>

<style>
	.nothing {
		text-align: center;
	}

	.tit {
		color: #007AFF;
		margin: 15rpx 10rpx;
		font-size: 45rpx;
		border-bottom: 1px solid #007AFF;
	}

	.image-bg {
		margin-top: 20rpx;
	}

	.top {
		height: 200rpx;
		/* margin: 2px 8px; */
		margin-right: 20rpx;
	}

	.name {
		font-size: 45rpx;
		/* margin: 20rpx; */
		margin-left: 170rpx;
		margin-top: -150rpx;
	}

	.tr {
		width: 95%;
		margin-left: 20rpx;
		margin-right: 20rpx;
		border-radius: 10rpx;
		background-color: #F1F1F1;
		margin-top: 20rpx;
	}

	.td {
		margin-top: 10rpx;
		font-size: 28rpx;
		margin-left: 170rpx;
	}

	.td1 {
		margin-top: 10rpx;
		font-size: 25rpx;
		margin-left: 170rpx;
		color: #555555;
	}

	.imga {
		margin-top: 20rpx;
	}

	.clik {
		top: -135rpx;
		width: 130rpx;
		height: 50rpx;
		background-color: #FFFFFF;
		color: #007AFF;
		margin-right: -10rpx;
		font-size: 25rpx;
	}

	.clik1 {
		top: -145rpx;
		width: 130rpx;
		height: 50rpx;
		margin-right: -10rpx;
		font-size: 25rpx;
		background-color: #DFDFDF;
		margin-top: 20rpx;
	}
</style>
